package com.caiheng.api.service.sys.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.sys.SysNoticeMapper;
import com.caiheng.api.entity.estate.EstateRoomBangUser;
import com.caiheng.api.entity.sys.SysNotice;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.estate.EstateRoomBangUserService;
import com.caiheng.api.service.sys.SysNoticeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl extends BaseServiceImpl<SysNoticeMapper, SysNotice> implements SysNoticeService {

    @Resource
    private SysNoticeMapper sysNoticeMapper;
    @Resource
    private EstateRoomBangUserService estateRoomBangUserService;
    @Resource
    protected RedisTemplate<String, Object> redisTemplate;

    @Value("${web.imgPrefixOss}")
    private String imgPrefix;//图片前缀

    @Override
    public List<SysNotice> getList(int userId) {
        QueryWrapper<SysNotice> qwNotice = new QueryWrapper<>();
        qwNotice.eq("status","0");
        Integer[] types = { 1, 2};
        qwNotice.in("notice_type", Arrays.asList(types));

        List<Integer> estateIds = new ArrayList<>();
        List<EstateRoomBangUser> bangUsers = estateRoomBangUserService.getMyBandingList(userId);
        if(CollUtil.isNotEmpty(bangUsers)){
            for (EstateRoomBangUser bangUser : bangUsers){
                estateIds.add(bangUser.getEstateId());
            }
        }

        if(estateIds.size() > 0){
            qwNotice.and(wrapper->{
                wrapper.eq("estate_id",0).or().in("estate_id",estateIds);
            });
        }

        qwNotice.orderByDesc("create_time");
        List<SysNotice> notices = sysNoticeMapper.selectList(qwNotice);
        return notices;
    }

    @Override
    public List<SysNotice> getListByType(User user, int notice_type, int groupId, int estateId, int pageIndex, int pageSize) {
        QueryWrapper<SysNotice> qwNotice = new QueryWrapper<>();
        qwNotice.eq("status","0");
        if(notice_type <= 2){
            Integer[] types = { 1, 2};
            qwNotice.in("notice_type", Arrays.asList(types));
        } else{
            qwNotice.eq("notice_type", notice_type);
        }
        if(groupId > 0){
            qwNotice.eq("notice_group_id", groupId);
        }

        if(estateId > 0){
            qwNotice.eq("estate_id", estateId);
            qwNotice.orderByDesc("create_time");
            List<SysNotice> notices = sysNoticeMapper.getPageEntityList(qwNotice,pageIndex,pageSize);
            return notices;
        }
        else{
            List<Integer> estateIds = new ArrayList<>();
            List<EstateRoomBangUser> bangUsers = estateRoomBangUserService.getMyBandingList(user.getId());
            if(CollUtil.isNotEmpty(bangUsers)){
                for (EstateRoomBangUser bangUser : bangUsers){
                    estateIds.add(bangUser.getEstateId());
                }
            }

            if(estateIds.size() > 0){
                qwNotice.eq("estate_id",user.getEstateId());
                qwNotice.orderByDesc("create_time");
                List<SysNotice> notices = sysNoticeMapper.getPageEntityList(qwNotice,pageIndex,pageSize);
                return notices;
            }
        }
        return new ArrayList<>();
    }

    @Override
    public void resetDate() {
        QueryWrapper<SysNotice> qwNotice = new QueryWrapper<>();
        qwNotice.eq("status","0");
        Integer[] types = { 1, 2};
        qwNotice.in("notice_type", Arrays.asList(types));
        String key = "sys_notice_1_2";
        List<SysNotice> notices = sysNoticeMapper.selectList(qwNotice);
        redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(notices));
    }
}
